package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IEmployeeService employeeService;


    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        qo.setStatus(Customer.STATUS_COMMON);


        //判断subject是否包含admin或者经理的角色
        Subject subject = SecurityUtils.getSubject();
        //只能看自己的数据
        //只能看本部门的数据
        //可以看所有人的数据
        //只能看自己管辖人员的数据



        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //如果不是经理或者管理员就只看自己的角色
            //获取当前登录用户id
            Employee employee = UserContext.getCurrentUser();
            //根据当前员工id查询所跟进的客户
            qo.setSellerId(employee.getId());
        }

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageResult", pageInfo);

        //查询职业下拉框的数据
        List<SystemDictionaryItem> jobs =  systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs", jobs);
        //查询来源下拉框的数据
        List<SystemDictionaryItem> sources =  systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources", sources);
        //交流方式下拉框数据
        List<SystemDictionaryItem> communicationMethods =  systemDictionaryItemService.selectByParentSn("communicationMethod");
        model.addAttribute("ccts", communicationMethods);

        //根据角色编码查询拥有该角色的员工 Market Market_Manager
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sllers", sellers);

        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
